package m.jclecai.com.fragment.hit;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.follow.HotPeopleInfoActivity;
import m.jclecai.com.adapter.base.ViewHolerAdapter;
import m.jclecai.com.adapter.holder.ViewHolder;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.Response.ResHitList;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by Dinosa on 2017/11/12.
 */

public class HitFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener2{

    @BindView(R.id.prl_list_view)
    PullToRefreshListView mPrlListView;
    private int mFlag;
    private HitListAdapter mHitListAdapter;
    private int mPage=1;
    private String mType;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        mFlag = arguments.getInt("flag", 0);
        mType = arguments.getString("type", "nums");
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_hit;
    }

    @Override
    public void initView() {
        super.initView();

        mHitListAdapter = new HitListAdapter();
        mPrlListView.setAdapter(mHitListAdapter);
        mPrlListView.setOnRefreshListener(this);
        mPrlListView.setMode(PullToRefreshBase.Mode.BOTH);
    }

    @Override
    public void initData() {
        super.initData();
        //这里我们对Adapter注入数据；
        if(mFlag == 0){
          requestData();
        }
    }

    /**
     *  数据请求
     */
    public void requestData() {
        showDialog();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("type", "nums");
        paramsMap.put("flag", mFlag + "");
        paramsMap.put("page",mPage+"");
        HttpManager.postAsyn(Constant_Url.HIT_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay("网络连接异常!!!");
                if (mPrlListView != null) {
                    mPrlListView.onRefreshComplete();
                }
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                if (!TextUtils.isEmpty(response)) {
                    //这里我们对数据进行要给解析
                    ResHitList resHitList = FastOk.get(response, ResHitList.class);
                    if (resHitList != null && "0".equals(resHitList.getStatus())) {

                        List<ResHitList.HitBean> hitList = resHitList.getRecords();
                        int totalPages=Integer.parseInt(resHitList.getPages());

                        if(mPage==1){
                            mHitListAdapter.refresh(hitList);
                        }else{
                            mHitListAdapter.addData(hitList);
                        }

                        if(mPage<totalPages){
                            mPage+=1;
                        }
                    } else {
                        DisPlay(resHitList.getMessage());
                    }

                } else {
                    DisPlay("网络连接异常！！！");
                }
                mPrlListView.onRefreshComplete();
            }
        }, paramsMap);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        //下拉刷新；
        mPage=1;
        mHitListAdapter.clear();
        requestData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        //上拉加载；
        requestData();
    }


    public class HitListAdapter extends ViewHolerAdapter<HitViewHolder> {

        public List<ResHitList.HitBean> mData = new ArrayList<>();


        @Override
        public HitViewHolder onCreateViewHolder(int position, ViewGroup parent) {
            return new HitViewHolder(getActivity(), R.layout.item_hit_list);
        }

        @Override
        public void onBindViewHolder(int position, HitViewHolder hitViewHolder) {
            hitViewHolder.init(position,mData.get(position));
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        public void refresh(List<ResHitList.HitBean> data) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
        public void clear(){
            mData.clear();
            notifyDataSetChanged();
        }

        public void addData(List<ResHitList.HitBean> data){
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    public class HitViewHolder extends ViewHolder {

        @BindView(R.id.tv_position)
        TextView mTvPosition;
        @BindView(R.id.tv_nickname)
        TextView mTvNickname;
        @BindView(R.id.tv_history)
        TextView mTvHistory;
        @BindView(R.id.tv_is_follow)
        TextView mTvIsFollow;

        @BindView(R.id.tv_follow_number)
        TextView mTvFollowNumber;

        @BindView(R.id.iv_user_icon)
        ImageView mIvUserIcon;

        @BindView(R.id.iv_user_rank)
        ImageView mIvUserRank;

        @BindView(R.id.iv_icon_first)
        ImageView mIvIconFirst;
        public HitViewHolder(Context context, @LayoutRes int resource) {
            super(context, resource);
        }

        public void init(int position, final ResHitList.HitBean hitBean) {
            if(position == 0){
                mIvIconFirst.setVisibility(View.VISIBLE);
                mTvPosition.setVisibility(View.INVISIBLE);
                mIvIconFirst.setImageResource(R.drawable.image_hit_count_1);

            }else if(position == 1){

                mIvIconFirst.setVisibility(View.VISIBLE);
                mTvPosition.setVisibility(View.INVISIBLE);
                mIvIconFirst.setImageResource(R.drawable.image_hit_count_2);
            }else if(position == 2){

                mIvIconFirst.setVisibility(View.VISIBLE);
                mTvPosition.setVisibility(View.INVISIBLE);
                mIvIconFirst.setImageResource(R.drawable.image_hit_count_3);
            }else{
                mIvIconFirst.setVisibility(View.INVISIBLE);
                mTvPosition.setVisibility(View.VISIBLE);

                mTvPosition.setText((position+1)+"");
            }
            mTvNickname.setText(hitBean.getUsername());

            if("nums".equals(mType)){
                String military = hitBean.getMilitary();
                String[] split = military.split("/");
                mTvHistory.setText("近7天战绩:"+split[0]+"中"+split[1]);
            }else{
                mTvHistory.setText("盈利率:"+hitBean.getPayback()+"");
            }

            Glide.with(HitFragment.this).load(hitBean.getUser_face()).into(mIvUserIcon);
            //这里我们判断是否可以跟单；
            if ("0".equals(hitBean.getUser_project_follow())) {
                mTvFollowNumber.setVisibility(View.INVISIBLE);
                mTvIsFollow.setEnabled(false);
            }else{
                mTvIsFollow.setEnabled(true);
                mTvFollowNumber.setText(hitBean.getUser_project_follow());
                mTvFollowNumber.setVisibility(View.VISIBLE);
            }
            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(HotPeopleInfoActivity.getIntent(getActivity(),hitBean.getUser_id()));
                }
            });

            if (hitBean.getUser_rank()>0) {
                mIvUserRank.setVisibility(View.VISIBLE);
            }else{
                mIvUserRank.setVisibility(View.INVISIBLE);
            }

        }
    }
}
